package com.example.house

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.view.view.BaseMvvmActivity
import com.example.house.adapter.HouseAdapter
import com.example.house.databinding.ActivityHouseBinding
import com.example.house.entity.MyHouseEntity
import com.example.house.vm.HouseViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route (path = "/house/HouseActivity")
@AndroidEntryPoint
class HouseActivity : BaseMvvmActivity<ActivityHouseBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_house
    private val houseViewModel:HouseViewModel by  viewModels()
    val houseAdapter=HouseAdapter()
    override fun initView() {
        super.initView()
        mDataBinding.rvHouse.adapter=houseAdapter
        mDataBinding.rvHouse.layoutManager=LinearLayoutManager(this)
        mDataBinding.tvA.setOnClickListener {
            houseViewModel.getListData(1)
        }
        mDataBinding.tvB.setOnClickListener {
            houseViewModel.getListData(2)
        }
        mDataBinding.tvC.setOnClickListener {
            houseViewModel.getListData(3)
        }
        mDataBinding.tvD.setOnClickListener {
            houseViewModel.getListData(4)
        }
        mDataBinding.gotoZu.setOnClickListener {
            ARouter.getInstance().build( "/house/HouseManngerActivity")
                .navigation()
        }
        houseViewModel.mSuccessLiveData.observe(this){
            houseAdapter.data.clear()
            var myHouseEntity= mutableListOf<MyHouseEntity>()
            val map= it.data.groupBy {
                it.area//根据面积分组
            }
            val list = map.keys.sorted()//根据面积排序
            var index=1
            for (key in list) {
                myHouseEntity.add(MyHouseEntity(index,key,map.get(key)!!))
                index++
            }
            houseAdapter.addData(myHouseEntity)
            houseAdapter.notifyDataSetChanged()
        }

    }

    override fun onResume() {
        super.onResume()
        houseViewModel.getListData(1)
    }

}